﻿using System;
using System.ComponentModel.DataAnnotations;
using SelfService.Model.Models;
using WalkingTec.Mvvm.Core;

namespace SelfService.ViewModel.SysTaskVMs
{
    public partial class SysTaskVm : BaseCRUDVM<SysTask>
    {
        [Display(Name = "是否立即运行")]
        public bool IsRun { get; set; }
        
        public SysTaskVm()
        {
        }

        protected override void InitVM()
        {
        }

        public override void DoAdd()
        {           
            Entity.CreateTime = DateTime.Now;
            if (Entity.TaskType is TaskTypeEnum.单次任务)
            {
                if(Entity.NextRunTime < DateTime.Now) Entity.NextRunTime = DateTime.Now;
            }

            if (Entity.TaskType is TaskTypeEnum.周期任务)
            {
                Entity.NextRunTime = IsRun ? DateTime.Now : DateTime.Now.Add(Entity.Interval);
            }

            if (Entity.TaskType is TaskTypeEnum.定时任务)
            {
                switch (Entity.TimeType)
                {
                    case TimeTypeEnum.天:
                        Entity.NextRunTime = IsRun ? DateTime.Now : DateTime.Today.AddDays(1).Add(Entity.Interval);
                        break;
                    case TimeTypeEnum.周:
                        Entity.NextRunTime = IsRun ? DateTime.Now : DateTime.Today.AddDays(7).Add(Entity.Interval);
                        break;
                    case TimeTypeEnum.月:
                        Entity.NextRunTime = IsRun ? DateTime.Now : DateTime.Today.AddMonths(1).Add(Entity.Interval);
                        break;
                    case TimeTypeEnum.年:
                        Entity.NextRunTime = IsRun ? DateTime.Now : DateTime.Today.AddYears(1).Add(Entity.Interval);
                        break;
                    default:
                        throw new NotSupportedException("不支持的时间类型");
                }
            }
            base.DoAdd();
        }

        public override void DoEdit(bool updateAllFields = false)
        {
            base.DoEdit(updateAllFields);
        }

        public override void DoDelete()
        {
            base.DoDelete();
        }
    }
}
